package com.adcc.monitoragent.show.conf;

import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.adcc.monitoragent.bean.DatabaseBean;
import com.adcc.monitoragent.bean.MongoBean;
import com.adcc.monitoragent.bean.MqBean;
import com.adcc.monitoragent.bean.OsBean;
import com.adcc.monitoragent.bean.RedisBean;
import com.adcc.monitoragent.bean.ServiceBean;
import com.adcc.monitoragent.bean.ZookeeperBean;
import com.adcc.monitoragent.show.bean.ClusterBean;
import com.adcc.monitoragent.show.bean.ClusterDeployBean;
import com.adcc.monitoragent.show.bean.DeployBean;
import com.adcc.monitoragent.show.bean.LogBean;
import com.adcc.monitoragent.show.bean.ServiceGroupBean;
import com.adcc.monitoragent.show.bean.WatchBean;
import com.adcc.monitoragent.util.Constants;
import com.adcc.monitoragent.util.ProtertiesUtil;

public class MonitorRead {
	
	public static void initWatchBean(String path) {
		 try {
			 SAXReader saxReader = new SAXReader();
			 Document doc = saxReader.read(new File(path));
			 List<Element> list = doc.selectNodes("/root/nodes/nodes");
			 for (int i = 0; i < list.size(); i++) {
				Element o = list.get(i);
				String title = o.attributeValue("title");
				String name = o.attributeValue("name");
				ClusterBean clusterBean = new ClusterBean();
				ClusterDeployBean clusterDeployBean = new ClusterDeployBean();
				//集群
				WatchBean.getClusterListMap().put(name, clusterBean);
				WatchBean.getLogListMap().put(name, new LogBean());
				WatchBean.getClusterDeployListMap().put(name, clusterDeployBean);
				
				List<Element> hosts = o.selectNodes("nodes[@name=\"hosts\"]/nodes");
				//主机
				for (Element element : hosts) {
					String titleE = element.attributeValue("title");
					String nameE = element.attributeValue("name");
					OsBean osBean = new OsBean(nameE);
					//初始化状态
					osBean.setReciveTime(new Date());
					osBean.setSumStatus(Constants.SUM_STATUS_ERROR);
					osBean.setSumStatusMsg("初始化");
					osBean.setShowType(osBean.getFixType() + "_" + osBean.getSumStatus());
					osBean.setShowName();
					DeployBean deployBean = new DeployBean(nameE);
					deployBean.setOsBean(osBean);
					clusterBean.getOsMap().put(nameE, osBean);
					clusterDeployBean.getDeployMap().put(nameE, deployBean);
				}
				List<Element> services = o.selectNodes("nodes[@name=\"services\"]/nodes");
				//服务组
				for (Element element : services) {
					String titleE = element.attributeValue("title");
					String nameE = element.attributeValue("name");
					ServiceGroupBean group = new ServiceGroupBean(nameE);
					clusterBean.getServiceGroupMap().put(nameE, group);
					//服务
					Iterator<Element> it = element.elementIterator();  
			        // 遍历  
			        while (it.hasNext()) {  
			            Element e = it.next();  
			            String titleIt = e.attributeValue("title");
						String nameIt = e.attributeValue("name");
						ServiceBean serviceBean = new ServiceBean(nameIt);
						//初始化状态
						serviceBean.setReciveTime(new Date());
						serviceBean.setSumStatus(Constants.SUM_STATUS_ERROR);
						serviceBean.setSumStatusMsg("初始化");
						serviceBean.setShowType(serviceBean.getFixType() + "_"  + serviceBean.getSumStatus());
						serviceBean.setShowName(nameE);
						group.getServiceMap().put(nameIt, serviceBean);
						clusterDeployBean.getDeployMap().get(nameIt.split(":")[0]).
							getAppMap().put(nameIt, serviceBean);
			        }  
				}
				//mq
				List<Element> mq = o.selectNodes("nodes[@name=\"mq\"]/nodes");
				for (Element element : mq) {
					String titleE = element.attributeValue("title");
					String nameE = element.attributeValue("name");
					MqBean mqBean = new MqBean(nameE);
					//初始化状态
					mqBean.setReciveTime(new Date());
					mqBean.setSumStatus(Constants.SUM_STATUS_ERROR);
					mqBean.setSumStatusMsg("初始化");
					mqBean.setShowType(mqBean.getFixType() + "_"  + mqBean.getSumStatus());
					mqBean.setShowName();
					clusterBean.getMqMap().put(nameE, mqBean);
					clusterDeployBean.getDeployMap().get(nameE.split(":")[0]).
						getAppMap().put(nameE, mqBean);
				}
				//zk
				List<Element> zookeeper = o.selectNodes("nodes[@name=\"zookeeper\"]/nodes");
				for (Element element : zookeeper) {
					String titleE = element.attributeValue("title");
					String nameE = element.attributeValue("name");
					ZookeeperBean zookeeperBean = new ZookeeperBean(nameE);
					//初始化状态
					zookeeperBean.setReciveTime(new Date());
					zookeeperBean.setSumStatus(Constants.SUM_STATUS_ERROR);
					zookeeperBean.setSumStatusMsg("初始化");
					zookeeperBean.setShowType(zookeeperBean.getFixType() + "_"  + zookeeperBean.getSumStatus());
					zookeeperBean.setShowName();
					clusterBean.getZkMap().put(nameE, zookeeperBean);
					clusterDeployBean.getDeployMap().get(nameE.split(":")[0]).
						getAppMap().put(nameE, zookeeperBean);
				}
				//mongo
				List<Element> mongodb = o.selectNodes("nodes[@name=\"mongodb\"]/nodes");
				for (Element element : mongodb) {
					String titleE = element.attributeValue("title");
					String nameE = element.attributeValue("name");
					MongoBean mongoBean = new MongoBean(nameE);
					//初始化状态
					mongoBean.setReciveTime(new Date());
					mongoBean.setSumStatus(Constants.SUM_STATUS_ERROR);
					mongoBean.setSumStatusMsg("初始化");
					mongoBean.setShowType(mongoBean.getFixType() + "_"  + mongoBean.getSumStatus());
					mongoBean.setShowName();
					clusterBean.getMongoMap().put(nameE, mongoBean);
					clusterDeployBean.getDeployMap().get(nameE.split(":")[0]).
						getAppMap().put(nameE, mongoBean);
				}
				//redis
				List<Element> redis = o.selectNodes("nodes[@name=\"redis\"]/nodes");
				for (Element element : redis) {
					String titleE = element.attributeValue("title");
					String nameE = element.attributeValue("name");
					RedisBean redisBean = new RedisBean(nameE);
					//初始化状态
					redisBean.setReciveTime(new Date());
					redisBean.setSumStatus(Constants.SUM_STATUS_ERROR);
					redisBean.setSumStatusMsg("初始化");
					redisBean.setShowType(redisBean.getFixType() + "_"  + redisBean.getSumStatus());
					redisBean.setShowName();
					clusterBean.getRedisMap().put(nameE, redisBean);
					clusterDeployBean.getDeployMap().get(nameE.split(":")[0]).
						getAppMap().put(nameE, redisBean);
				}
				
				//database
				List<Element> database = o.selectNodes("nodes[@name=\"database\"]/nodes");
				for (Element element : database) {
					String titleE = element.attributeValue("title");
					String nameE = element.attributeValue("name");
					DatabaseBean databaseBean = new DatabaseBean(nameE);
					//初始化状态
					databaseBean.setReciveTime(new Date());
					databaseBean.setSumStatus(Constants.SUM_STATUS_ERROR);
					databaseBean.setSumStatusMsg("初始化");
					databaseBean.setShowType(databaseBean.getFixType() + "_"  + databaseBean.getSumStatus());
					databaseBean.setShowName();
					clusterBean.getDbMap().put(nameE, databaseBean);
					clusterDeployBean.getDeployMap().get(nameE.split(":")[0]).
						getAppMap().put(nameE, databaseBean);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		ProtertiesUtil.init();
		String path = ProtertiesUtil.getInitNodeConfig();
		initWatchBean(path);
		ClusterBean bean = WatchBean.getClusterOne();
		// TODO Auto-generated method stub
		 SAXReader saxReader = new SAXReader();
		 Document doc = saxReader.read(new File(path));
		 Element root = doc.getRootElement();
		 System.out.println(root.attributeValue("isDynamic"));
		 List<Element> list = doc.selectNodes("/root/nodes/nodes");
		 for (int i = 0; i < list.size(); i++) {
			Element o = list.get(i);
			System.out.println(o.attributeValue("title") + o.attributeValue("name") + o.attributeValue("isenabled"));
			List<Element> hosts = o.selectNodes("nodes[@name=\"hosts\"]/nodes");
			for (Element element : hosts) {
				System.out.println(element.attributeValue("title") + element.attributeValue("name"));
			}
			List<Element> services = o.selectNodes("nodes[@name=\"services\"]/nodes");
			for (Element element : services) {
				System.out.println(element.attributeValue("title") + element.attributeValue("name"));
				Iterator<Element> it = element.elementIterator();  
		        // 遍历  
		        while (it.hasNext()) {  
		            Element e = it.next();  
		            System.out.println(e.attributeValue("title") + e.attributeValue("name"));
		        }  
			}
			List<Element> mq = o.selectNodes("nodes[@name=\"mq\"]/nodes");
			for (Element element : mq) {
				System.out.println(element.attributeValue("title") + element.attributeValue("name"));
			}
			List<Element> zookeeper = o.selectNodes("nodes[@name=\"zookeeper\"]/nodes");
			for (Element element : zookeeper) {
				System.out.println(element.attributeValue("title") + element.attributeValue("name"));
			}
			List<Element> mongodb = o.selectNodes("nodes[@name=\"mongodb\"]/nodes");
			for (Element element : mongodb) {
				System.out.println(element.attributeValue("title") + element.attributeValue("name"));
			}
		}
	}

}
